﻿using Abp.AspNetCore.Mvc.Controllers;
using Magicodes.AppSession;
using Magicodes.WeChat.Web.Core.MvcExtension.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Magicodes.WeChat.Web.Core
{
    /// <summary>
    /// 微信授权
    /// </summary>
    [AllowAnonymous]
    [WeChatOAuth]
    public class WeChatOauthPageController : AppBaseController
    {
        public WeChatOauthPageController() : base(WeChatConsts.LocalizationSourceName)
        {
        }

        /// <summary>
        /// 微信授权获取粉丝信息
        /// </summary>
        /// <param name="redirectUrl"></param>
        /// <returns></returns>

        [HttpGet("WeChatOauthPage")]
        public ActionResult OauthAndRedirect(string redirectUrl)
        {
            if (string.IsNullOrWhiteSpace(redirectUrl))
            {
                return Content("redirectUrl是必须的!");
            }
            if (AppSession.AppSessionInfo.RequestType != RequestTypes.WeChat)
            {
                return Content("不支持除微信浏览器以外的请求!");
            }
            if (redirectUrl.Contains("openid="))
            {
                return Redirect(redirectUrl);
            }
            redirectUrl += redirectUrl.Contains("?") ? "&" : "?";
            redirectUrl += "openid=" + AppSession.AppSessionInfo.AppUserId;
            return Redirect(redirectUrl);
        }
    }
}
